package com.zenithmind.document.pojo.query;

import com.zenithmind.common.pojo.query.BaseQueryDTO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 文档权限查询DTO
 * 遵循单一职责原则：专注于权限查询条件
 * 遵循开闭原则：支持查询条件的扩展
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Schema(description = "文档权限查询DTO")
public class DocumentPermissionQueryDTO extends BaseQueryDTO {

    @Schema(description = "文档ID", example = "doc123456")
    private String documentId;

    @Schema(description = "文档ID列表")
    private List<String> documentIds;

    @Schema(description = "目标类型", example = "USER", allowableValues = {"USER", "ROLE", "GROUP", "DEPARTMENT"})
    private String targetType;

    @Schema(description = "目标类型列表")
    private List<String> targetTypes;

    @Schema(description = "目标ID", example = "user123456")
    private String targetId;

    @Schema(description = "目标ID列表")
    private List<String> targetIds;

    @Schema(description = "目标名称", example = "张三")
    private String targetName;

    @Schema(description = "目标名称关键词", example = "张")
    private String targetNameKeyword;

    @Schema(description = "权限类型", example = "READ", allowableValues = {"read", "write", "delete", "share", "admin"})
    private String permissionType;

    @Schema(description = "权限类型列表")
    private List<String> permissionTypes;

    @Schema(description = "权限值", example = "ALLOW", allowableValues = {"ALLOW", "DENY", "INHERIT"})
    private String permissionValue;

    @Schema(description = "权限值列表")
    private List<String> permissionValues;

    @Schema(description = "最小权限级别", example = "1")
    private Integer minPermissionLevel;

    @Schema(description = "最大权限级别", example = "5")
    private Integer maxPermissionLevel;

    @Schema(description = "权限范围", example = "DOCUMENT", allowableValues = {"DOCUMENT", "FOLDER", "CATEGORY", "GLOBAL"})
    private String permissionScope;

    @Schema(description = "权限范围列表")
    private List<String> permissionScopes;

    @Schema(description = "是否继承", example = "true")
    private Boolean isInherited;

    @Schema(description = "父权限ID", example = "perm123456")
    private String parentPermissionId;

    @Schema(description = "父权限ID列表")
    private List<String> parentPermissionIds;

    @Schema(description = "最小优先级", example = "100")
    private Integer minPriority;

    @Schema(description = "最大优先级", example = "500")
    private Integer maxPriority;

    @Schema(description = "生效时间开始", example = "2025-01-09T00:00:00")
    private LocalDateTime effectiveTimeStart;

    @Schema(description = "生效时间结束", example = "2025-01-09T23:59:59")
    private LocalDateTime effectiveTimeEnd;

    @Schema(description = "过期时间开始", example = "2025-12-31T00:00:00")
    private LocalDateTime expirationTimeStart;

    @Schema(description = "过期时间结束", example = "2025-12-31T23:59:59")
    private LocalDateTime expirationTimeEnd;

    @Schema(description = "授权人ID", example = "admin123")
    private String grantorId;

    @Schema(description = "授权人ID列表")
    private List<String> grantorIds;

    @Schema(description = "授权原因关键词", example = "项目")
    private String grantReasonKeyword;

    @Schema(description = "权限标签", example = "临时权限")
    private String permissionTags;

    @Schema(description = "权限标签关键词", example = "临时")
    private String permissionTagsKeyword;

    @Schema(description = "是否可委托", example = "true")
    private Boolean isDelegatable;

    @Schema(description = "最小委托层级", example = "1")
    private Integer minDelegationLevel;

    @Schema(description = "最大委托层级", example = "5")
    private Integer maxDelegationLevel;

    @Schema(description = "审批状态", example = "APPROVED", allowableValues = {"PENDING", "APPROVED", "REJECTED", "REVOKED"})
    private String approvalStatus;

    @Schema(description = "审批状态列表")
    private List<String> approvalStatuses;

    @Schema(description = "审批人ID", example = "approver123")
    private String approverId;

    @Schema(description = "审批人ID列表")
    private List<String> approverIds;

    @Schema(description = "审批时间开始", example = "2025-01-09T00:00:00")
    private LocalDateTime approvalTimeStart;

    @Schema(description = "审批时间结束", example = "2025-01-09T23:59:59")
    private LocalDateTime approvalTimeEnd;

    @Schema(description = "审批意见关键词", example = "同意")
    private String approvalCommentKeyword;

    @Schema(description = "权限描述关键词", example = "查看")
    private String descriptionKeyword;

    @Schema(description = "扩展属性关键词", example = "department")
    private String extendedPropertiesKeyword;

    @Schema(description = "备注关键词", example = "临时")
    private String remarksKeyword;

    @Schema(description = "是否启用", example = "true")
    private Boolean isEnabled;

    @Schema(description = "状态", example = "ACTIVE", allowableValues = {"ACTIVE", "INACTIVE", "SUSPENDED", "EXPIRED"})
    private String status;

    @Schema(description = "状态列表")
    private List<String> statuses;

    @Schema(description = "是否已过期", example = "false")
    private Boolean isExpired;

    @Schema(description = "是否即将过期", example = "false")
    private Boolean isExpiringSoon;

    @Schema(description = "即将过期天数", example = "7")
    private Integer expiringSoonDays;

    @Schema(description = "是否临时权限", example = "true")
    private Boolean isTemporary;

    @Schema(description = "是否有权限条件", example = "true")
    private Boolean hasPermissionCondition;

    @Schema(description = "权限条件关键词", example = "timeRange")
    private String permissionConditionKeyword;

    @Schema(description = "创建时间开始", example = "2025-01-09T00:00:00")
    private LocalDateTime createTimeStart;

    @Schema(description = "创建时间结束", example = "2025-01-09T23:59:59")
    private LocalDateTime createTimeEnd;

    @Schema(description = "更新时间开始", example = "2025-01-09T00:00:00")
    private LocalDateTime updateTimeStart;

    @Schema(description = "更新时间结束", example = "2025-01-09T23:59:59")
    private LocalDateTime updateTimeEnd;

    @Schema(description = "排序字段", example = "createTime", allowableValues = {
            "createTime", "updateTime", "effectiveTime", "expirationTime", "priority", "permissionLevel"
    })
    private String sortField;

    @Schema(description = "排序方向", example = "DESC", allowableValues = {"ASC", "DESC"})
    private String sortDirection;

    @Schema(description = "分组字段", example = "targetType", allowableValues = {
            "targetType", "permissionType", "permissionValue", "permissionScope", "approvalStatus", "status"
    })
    private String groupBy;

    @Schema(description = "统计类型", example = "COUNT", allowableValues = {"COUNT", "SUM", "AVG", "MAX", "MIN"})
    private String statisticsType;

    @Schema(description = "是否包含子权限", example = "true")
    private Boolean includeChildPermissions;

    @Schema(description = "是否只查询有效权限", example = "true")
    private Boolean onlyEffectivePermissions;

    @Schema(description = "是否只查询直接权限", example = "false")
    private Boolean onlyDirectPermissions;

    /**
     * 验证查询条件
     */
    public boolean isValid() {
        // 时间范围验证
        if (effectiveTimeStart != null && effectiveTimeEnd != null) {
            if (effectiveTimeStart.isAfter(effectiveTimeEnd)) return false;
        }
        
        if (expirationTimeStart != null && expirationTimeEnd != null) {
            if (expirationTimeStart.isAfter(expirationTimeEnd)) return false;
        }
        
        if (approvalTimeStart != null && approvalTimeEnd != null) {
            if (approvalTimeStart.isAfter(approvalTimeEnd)) return false;
        }
        
        if (createTimeStart != null && createTimeEnd != null) {
            if (createTimeStart.isAfter(createTimeEnd)) return false;
        }
        
        if (updateTimeStart != null && updateTimeEnd != null) {
            if (updateTimeStart.isAfter(updateTimeEnd)) return false;
        }
        
        // 数值范围验证
        if (minPermissionLevel != null && maxPermissionLevel != null) {
            return minPermissionLevel <= maxPermissionLevel;
        }
        
        if (minPriority != null && maxPriority != null) {
            return minPriority <= maxPriority;
        }
        
        if (minDelegationLevel != null && maxDelegationLevel != null) {
            return minDelegationLevel <= maxDelegationLevel;
        }
        
        return true;
    }

    /**
     * 是否有时间范围查询
     */
    public boolean hasTimeRangeQuery() {
        return effectiveTimeStart != null || effectiveTimeEnd != null ||
               expirationTimeStart != null || expirationTimeEnd != null ||
               approvalTimeStart != null || approvalTimeEnd != null ||
               createTimeStart != null || createTimeEnd != null ||
               updateTimeStart != null || updateTimeEnd != null;
    }

    /**
     * 是否有权限级别查询
     */
    public boolean hasPermissionLevelQuery() {
        return minPermissionLevel != null || maxPermissionLevel != null;
    }

    /**
     * 是否有优先级查询
     */
    public boolean hasPriorityQuery() {
        return minPriority != null || maxPriority != null;
    }

    /**
     * 是否有委托查询
     */
    public boolean hasDelegationQuery() {
        return isDelegatable != null || minDelegationLevel != null || maxDelegationLevel != null;
    }

    /**
     * 是否有审批查询
     */
    public boolean hasApprovalQuery() {
        return approvalStatus != null || approverId != null || 
               approvalTimeStart != null || approvalTimeEnd != null ||
               approvalCommentKeyword != null;
    }

    /**
     * 是否有关键词查询
     */
    public boolean hasKeywordQuery() {
        return targetNameKeyword != null || grantReasonKeyword != null ||
               permissionTagsKeyword != null || approvalCommentKeyword != null ||
               descriptionKeyword != null || extendedPropertiesKeyword != null ||
               remarksKeyword != null || permissionConditionKeyword != null;
    }

    /**
     * 获取默认排序
     */
    public String getDefaultSort() {
        return "createTime DESC";
    }

    /**
     * 是否查询即将过期的权限
     */
    public boolean isQueryExpiringSoon() {
        return isExpiringSoon != null && isExpiringSoon;
    }

    /**
     * 获取即将过期的截止时间
     */
    public LocalDateTime getExpiringSoonDeadline() {
        int days = expiringSoonDays != null ? expiringSoonDays : 7;
        return LocalDateTime.now().plusDays(days);
    }
}
